package com.mayikt.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @ClassName JwtTest003
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class JwtTest003 {
    private static final String SIGN_KEY = "mayiktSignKey";
    public static void main(String[] args) {
        //当前时间
        long now = System.currentTimeMillis();
        //过期时间为1分钟
        long exp = now + 1000 * 1;
        JwtBuilder builder = Jwts.builder()
                .setIssuedAt(new Date())
                .claim("userId", "1234")
                .signWith(SignatureAlgorithm.HS256, SIGN_KEY)
                .setExpiration(new Date(exp));//用于设置过期时间
        System.out.println(builder.compact());

        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
        Claims body = Jwts.parser().setSigningKey(SIGN_KEY).parseClaimsJws(builder.compact()
        ).getBody();
        System.out.println(body);
    }
}
